function angles = generate_user_angles(K, P, psi_g, theta_g, delta_psi, delta_theta)
% GENERATE_USER_ANGLES 生成用户角度信息（文献Section II）
%   输入参数：
%     K: 总用户数
%     P: 每条路径数
%     psi_g: 组平均方位角向量 [1×G] (度)
%     theta_g: 组平均仰角向量 [1×G] (度)
%     delta_psi: 方位角扩展 (度)
%     delta_theta: 仰角扩展 (度)
%
%   输出结构体数组angles [1×K]:
%     user_id: 用户ID
%     group_id: 所属组ID
%     azimuth_angles: 方位角矩阵 [P×1] (度)
%     elevation_angles: 仰角矩阵 [P×1] (度)
%     gamma_x: 水平波数向量 [P×1]
%     gamma_y: 垂直波数向量 [P×1]

    % ========== 1. 参数验证 ==========
    validate_parameters(K, P, psi_g, theta_g, delta_psi, delta_theta);
    
    % ========== 2. 初始化输出结构 ==========
    angles = struct('user_id', {}, 'group_id', {}, ...
                    'azimuth_angles', {}, 'elevation_angles', {}, ...
                    'gamma_x', {}, 'gamma_y', {});
    
    % ========== 3. 确定组分配 ==========
    G = length(psi_g);  % 总组数
    users_per_group = floor(K / G);  % 每组基础用户数
    remainder = mod(K, G);  % 剩余用户
    
    % ========== 4. 生成用户角度 ==========
    user_idx = 1;
    for g = 1:G
        % 4.1 确定当前组用户数
        if g <= remainder
            group_size = users_per_group + 1;
        else
            group_size = users_per_group;
        end
        
        % 4.2 生成组内用户
        for u = 1:group_size
            % 4.2.1 生成方位角（均匀分布在扩展范围内）
            psi_min = psi_g(g) - delta_psi;
            psi_max = psi_g(g) + delta_psi;
            azimuth = psi_min + (psi_max - psi_min) * rand(P, 1);
            
            % 4.2.2 生成仰角（均匀分布在扩展范围内）
            theta_min = theta_g(g) - delta_theta;
            theta_max = theta_g(g) + delta_theta;
            elevation = theta_min + (theta_max - theta_min) * rand(P, 1);
            
            % 4.2.3 计算波数参数（公式1）
            gamma_x = sind(elevation) .* cosd(azimuth);
            gamma_y = sind(elevation) .* sind(azimuth);
            
            % 4.2.4 存储结果
            angles(user_idx).user_id = user_idx;
            angles(user_idx).group_id = g;
            angles(user_idx).azimuth_angles = azimuth;
            angles(user_idx).elevation_angles = elevation;
            angles(user_idx).gamma_x = gamma_x;
            angles(user_idx).gamma_y = gamma_y;
            
            user_idx = user_idx + 1;
        end
    end
    
    % ========== 5. 验证输出 ==========
    assert(user_idx-1 == K, 'Generated user count mismatch');
end

%% ========== 子函数：参数验证 ==========
function validate_parameters(K, P, psi_g, theta_g, delta_psi, delta_theta)
    % 参数类型验证
    if ~isscalar(K) || K <= 0 || mod(K,1) ~= 0
        error('K must be a positive integer');
    end
    if ~isscalar(P) || P <= 0 || mod(P,1) ~= 0
        error('P must be a positive integer');
    end
    if ~isvector(psi_g) || ~isnumeric(psi_g)
        error('psi_g must be a numeric vector');
    end
    if ~isvector(theta_g) || ~isnumeric(theta_g)
        error('theta_g must be a numeric vector');
    end
    if length(psi_g) ~= length(theta_g)
        error('psi_g and theta_g must have same length');
    end
    if ~isscalar(delta_psi) || delta_psi <= 0
        error('delta_psi must be a positive scalar');
    end
    if ~isscalar(delta_theta) || delta_theta <= 0
        error('delta_theta must be a positive scalar');
    end
    
    % 角度范围验证
    if any(psi_g < 0 | psi_g > 360)
        error('psi_g must be in [0,360] degrees');
    end
    if any(theta_g < 0 | theta_g > 90)
        error('theta_g must be in [0,90] degrees');
    end
end